<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table.js"></script>
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table-zh-CN.min.js"></script>
    <script src="__STATIC__/bootstrap-table/js/dropdown.js"></script>
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap-table.min.css">
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap.css">
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
    <script src="__STATIC__/js/common.js"></script>
    <style>
        input.layui-input.layui-unselect {
            width: 200px;
        }
    </style>
</head>
<body>

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">设备管理</a>
        <a href="#">设备列表</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="layui-row">
    <form class="layui-form layui-col-md12 x-so" onsubmit="return false">
        <input type="text" name="username"  placeholder="设备编号" autocomplete="off" class="layui-input">
        <div class="layui-input-inline layui-show-xs-block" >
            <select name="school_id" id="school_id" style="width: 500px">
                <option value="">请选择学校</option>
                {foreach $school as $info}
                    <option value="{$info.school_id}">{$info.school_name}</option>
                {/foreach}
            </select>
        </div>
        <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></button>
    </form>
</div>
<xblock>
    <button class="layui-btn" onclick="add_equipment()">添加设备</button>
    <button class="layui-btn layui-btn-normal" id="binding">绑定</button>
    <button class="layui-btn layui-btn-danger" id="untying">解绑</button>
    <button class="layui-btn layui-btn-primary" id="import">导入设备信息</button>
    <button class="layui-btn layui-btn-warm" id="open_monitoring">开启检测模式</button>
    <button class="layui-btn layui-btn-danger" onclick="" id="close_monitoring">关闭检测模式</button>
    <button class="layui-btn layui-btn-primary" id="number">下发编号</button>
</xblock>
<table id="equipmentList" class="table table-bordered table-hover table-striped">

</table>
<div style="width: 400px;display: none;padding-top: 5px" id="form">
    <form class="layui-form" action="" name="equipment" onsubmit="return false" id="equipment">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">用户信息</label>
            <div class="layui-input-inline">
                <input type="text" name="info" id="info" placeholder="用户信息" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><span style="color: red">家长:家长手机号否则学校全称</span></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">绑定对象</label>
            <div class="layui-input-block">
                <input type="radio" name="bindings" value="1" title="家长" checked>
                <input type="radio" name="bindings" value="2" title="学校">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="equipment_binding">立即绑定</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<div style="width: 400px;display: none;padding-top: 5px" id="form1">
    <form class="layui-form" action="" name="equipment1" onsubmit="return false" id="equipment1">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">绑定对象</label>
            <div class="layui-input-block">
                <input type="radio" name="bindings" value="1" title="家长" checked>
                <input type="radio" name="bindings" value="2" title="学校">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="equipment_untying">立即解绑</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<div style="width: 400px;display: none;padding-top: 5px" id="form2">
    <form class="layui-form" action="" name="equipment2" onsubmit="return false" id="equipment2">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">批次号</label>
            <div class="layui-input-block">
                <input type="text" name="batch" style="width: 150px" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="lower_number">立即下发</button>
            </div>
        </div>
    </form>
</div>
<div style="width: 400px;display: none;padding-top: 30px" id="form3">
    <form class="layui-form" action="" name="equipment3" onsubmit="return false" id="equipment3">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">批次号</label>
            <div class="layui-input-block">
                <input type="text" name="batch" style="width: 150px" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" onclick="close_monitoring()">立即下发</button>
            </div>
        </div>
    </form>
</div>
<div style="width: 400px;display: none;padding-top: 30px" id="form4">
    <form class="layui-form" action="" name="equipment4" onsubmit="return false" id="equipment4">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">批次号</label>
            <div class="layui-input-block">
                <input type="text" name="batch" style="width: 150px" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" onclick="open_monitoring()">立即下发</button>
            </div>
        </div>
    </form>
</div>
</body>
<script>
    layui.use('upload',function(){
        var upload = layui.upload;
        upload.render({
            elem: '#import'
            ,url: "{:url('/equipment/importEquipmentInfo')}"
            ,field:'excel'
            ,accept: 'file'
            ,exts:"xls|xlsx"
            ,done: function(res, index, upload){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        $("#equipmentList").bootstrapTable(('refresh'));
                    });
                }else{
                    layer.msg(res.msg,{icon:5,time:2000})
                }
            }
        })
    });
</script>
<script>
    $('#equipmentList').bootstrapTable({
        //获取行号
        language: 'zh-CN',
        todayHighlight: true,
        striped: true,//设置为 true 会有隔行变色效果
        method: 'post',//服务器数据的请求方式 'get' or 'post'
        url: "{:url('/equipment/equipmentList')}",//服务器数据的加载地址
        toolbar:'#toolbar',//
        searchOnEnterKey:true,
        sortName:'ID',//排序字段
        sortOrder : 'desc',//降序
        sidePagination: "server",   //分页方式：client客户端分页，server服务端分页（*）
        pagination: 'false',//设置为 true 会在表格底部显示分页条
       // clickToSelect:'true',//设置true 将在点击行时，自动选择rediobox 和 checkbox
        //singleSelect:'true',//设置True 将禁止多选
        pageSize: '15',//如果设置了分页，页面数据条数
        pageList:[15,30,50],
        smartDisplay:false,
        //得到查询的参数
        queryParams : function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var temp = {
                rows: params.limit,                         //页面大小
                page: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order,//排位命令（desc，asc）
                searchName:$("input[name=username]").val(),
                school_id:$("#school_id option:selected").val()
            };
            return temp;
        },
        columns: [
            {title:'请选择',align:'center',checkbox : true},
            {field: 'id', title: 'id号', align:'center'},
            {field: 'number', title: '设备编号', align:'center'},
            {field: 'serial_number', title: '座位号', align:'center'},
            {field: 'device_batch', title: '批次(用于后台)', align:'center'},
            {field: 'batch', title: '批次(用于学校端)', align:'center'},
            {title: '是否绑定家长', align:'center',formatter:parents},
            {title: '是否绑定学校', align:'center',formatter:school},
            {align:'center', title:'操作', formatter:operateFormatter}
        ]}
    );
    //操作按钮
    function operateFormatter(val,rows) {
        return [
            "<button type='button' onclick='edit("+rows.id+")' class='btn btn-info btn-xs'>编辑</button>&nbsp;&nbsp;",
            "<button type='button' onclick='communication("+rows.number+")' class='btn btn-success btn-xs'>通讯记录</button>&nbsp;&nbsp;",
            "<button type='button' onclick='status("+rows.number+")' class='btn btn-warning btn-xs'>设备状态</button>&nbsp;&nbsp;",
            "<button type='button' onclick='del("+rows.id+")' class='btn btn-danger btn-xs'>删除</button>"
        ].join('');
    }
    //是否绑定家长
    function parents(val,rows){
        if(rows.parents){
            return rows.parents.phone;
        }else{
            return "<span style='color: red'>未绑定</span>"
        }
    }
    //是否绑定学校
    function school(val,rows){
        if(rows.school){
            return rows.school.school_name;
        }else{
            return "<span style='color: red'>未绑定</span>"
        }
    }
    //添加设备
    function add_equipment() {
        var url = '/equipment/add';
        add(url)
    }
    //编辑页面
    function edit(id){
        var url = '/equipment/edit';
        editInfo(url,id);
    }
    //通讯记录
    function communication(number){
        window.location.href = "{:url('/equipment/communication')}?number="+number+"";
    }
    //设备状态
    function status(number){
        window.location.href = "{:url('/equipment/status')}?number="+number;
    }
    //删除设备
    function del(id){
        var url = '/equipment/deleteEquipment';
        var bootstrapTableId = 'equipmentList';
        deleteInfo(url,id,bootstrapTableId);
    }
    //家长解绑
    function parent_untying(id){
        var url = '/equipment/parentsUntying';
        var bootstrapTableId = 'equipmentList';
        untying(id,url,bootstrapTableId);
    }
    //学校解绑
    function school_untying(id){
        var url = '/equipment/schoolUntying';
        var bootstrapTableId = 'equipmentList';
        untying(id,url,bootstrapTableId);
    }
    //绑定页面
    $("#binding").click(function(){
        layer.open({
            title:"设备绑定",
            type:1,
            area:['500px','350px'],
            content:$("#form")
        });
    });
    //设备绑定操作
    $("#equipment_binding").click(function(){
        var id = [];
        var info = $("#equipmentList").bootstrapTable('getSelections');
        for (var i=0;i<info.length;i++){
            id.push(info[i].id);
        }
        if(id.length == 0){
            layer.msg('请选择编号',{icon:5,time:2000})
            return
        }
        $("input[name=id]").val(id);
        if(!$("#info").val()){
            layer.msg('请输入用户信息',{icon:5,time:2000});
            return;
        }
        $.ajax({
            url:"{:url('/equipment/binding')}",
            data:$("#equipment").serialize(),
            type:'post',
            dataType:"json",
            success:function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        $("#equipmentList").bootstrapTable(('refresh'));
                        window.equipment.reset();
                        layer.closeAll();
                    });

                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                }
            }
        });
    });
    //解绑页面
    $("#untying").click(function(){
        layer.open({
            title:"设备解绑",
            type:1,
            area:['500px','350px'],
            content:$("#form1")
        });
    });
    //设备批量解绑操作
    $("#equipment_untying").click(function(){
        var id = [];
        var info = $("#equipmentList").bootstrapTable('getSelections');
        for (var i=0;i<info.length;i++){
            id.push(info[i].id);
        }
        if(id.length == 0){
            layer.msg('请选择编号',{icon:5,time:2000})
            return
        }
        $("input[name=id]").val(id);

        $.ajax({
            url:"{:url('/equipment/untying')}",
            data:$("#equipment1").serialize(),
            type:'post',
            dataType:"json",
            success:function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        $("#equipmentList").bootstrapTable(('refresh'));
                        window.equipment1.reset();
                        layer.closeAll();
                    });
                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                }
            }
        });
    });
    //下发编号页面
    $("#number").click(function () {
        layer.open({
            title:"下发编号",
            type:1,
            area:['400px','300px'],
            content:$("#form2")
        });
    })
    //下发编号
    $("#lower_number").click(function () {
        $.ajax({
            url:"{:url('/equipment/lower_number')}",
            data:$("#equipment2").serialize(),
            type:'post',
            dataType:"json",
            success:function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        layer.closeAll();
                    });
                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                }
            }
        });
    })
    //关闭检测模式页面
    $("#close_monitoring").click(function () {
        layer.open({
            title:"关闭检测模式",
            type:1,
            area:['400px','300px'],
            content:$("#form3")
        });
    })
    //下发关闭检测模式
    function close_monitoring() {
        $.ajax({
            url:"{:url('/equipment/close_monitoring')}",
            data:$("#equipment3").serialize(),
            type:'post',
            dataType:"json",
            success:function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        layer.closeAll();
                    });
                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                }
            }
        });
    }
    //开启检测模式
    $("#open_monitoring").click(function () {
        layer.open({
            title:"开启检测模式",
            type:1,
            area:['400px','300px'],
            content:$("#form4")
        });
    })
    //下发检测模式
    function open_monitoring() {
        $.ajax({
            url:"{:url('/equipment/open_monitoring')}",
            data:$("#equipment4").serialize(),
            type:'post',
            dataType:"json",
            success:function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        layer.closeAll();
                    });
                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                }
            }
        });
    }
    //检索
    $("#search").click(function () {
        $("#equipmentList").bootstrapTable(('refresh'))
    })
</script>
</html>